package com.tul.exercise.base.java8.stream.base;

import com.tul.exercise.base.java8.common.Constant;
import com.tul.exercise.base.java8.util.BeanAtributeHelper;
import com.tul.exercise.base.utils.MathUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 专辑类
 * @author tuliang
 */
public class Album {
    /**
     * 专辑名称
     */
    private String name;

    /**
     * 曲目
     */
    private List<Track> tracks;

    /**
     * 演唱者
     */
    private List<Artist> musicians;

    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Track> getTrackList() {
        return tracks;
    }

    public void setTrackList(List<Track> tracks) {
        this.tracks = tracks;
    }

    public Stream<Track> getTracks() {
        return tracks.stream();
    }

    public void setTracks(Stream<Track> tracks) {
        this.tracks = tracks.collect(Collectors.toList());
    }

    public List<Artist> getMusicians() {
        return musicians;
    }

    public void setMusicians(List<Artist> musicians) {
        this.musicians = musicians;
    }

    /**
     * 创建专辑
     * @return 专辑
     */
    public static Album createAlbum() {
        Album album = new Album();
        String name = BeanAtributeHelper.getRandomItem(Constant.APPLE_NAMES);
        album.setName(name);

        List<Track> tracks = Track.createTracks(MathUtils.getRandomInt(0, 100));
        album.setTrackList(tracks);

        List<Artist> artists = Artist.createArtists(MathUtils.getRandomInt(1,99));
        album.setMusicians(artists);

        return album;
    }

    /**
     * 创建多个专辑
     * @param num 个数
     * @return 专辑列表
     */
    public static List<Album> createAlbums(int num) {
        List<Album> list = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            list.add(createAlbum());
        }
        return list;
    }
}
